//
//  VoiceAssistantSettingViewController.m
//  VoiceAssistant
//
//  Created by GJS on 2020/9/18.
//  Copyright © 2020 G. All rights reserved.
//

#import "VoiceAssistantSetting.h"
#import "SSKit.h"

#define kHighlightColor makeColorHex(@"#0080FF")
#define kNormalColor UIColor.whiteColor

@interface VoiceAssistantSetting ()

@property (nonatomic, strong) NSMutableDictionary *settingDict;
@property (nonatomic, strong) UIButton *btnVoiceSpeed1;
@property (nonatomic, strong) UIButton *btnVoiceSpeed2;
@property (nonatomic, strong) UIButton *btnVoiceSpeed3;
@property (nonatomic, strong) UIButton *btnVoiceTag1;
@property (nonatomic, strong) UIButton *btnVoiceTag2;

@end

@implementation VoiceAssistantSetting

+ (NSMutableDictionary *)settingDict {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:kVoiceAssistantSetting] ?: @{
        kSettingVoiceOn: @YES,
        kSettingVoiceSpeed: @1,
        kSettingVoiceTag: @"女声",
    } mutableCopy];
}

- (NSMutableDictionary *)settingDict {
    if (_settingDict == nil) {
        _settingDict = [self.class settingDict];
    }
    return _settingDict;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"语音助手设置";
    //self.navigationController.navigationItem.leftBarButtonItem = [self leftBarItem];
    [self.view addSubview:[self formView1]];
    [self.view addSubview:[self formView2]];
    [self.view addSubview:[self formView3]];
}

- (UIBarButtonItem *)leftBarItem {
    //UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleDone target:self action:@selector(onBack:)];
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onBack:)];
    
    return backBarButtonItem;
}

- (void)onBack:(id)sender {
    [self dismissViewControllerAnimated:YES  completion:nil];
}

- (UIView *)formView1 {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 48)];
    
    UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(kScreenWidth - 51 - 16, 0, 51, 31)];
    switchBtn.center = CGPointMake(switchBtn.center.x, view.frame.size.height / 2);
    switchBtn.onTintColor = kHighlightColor;
    switchBtn.tag = 11;
    [switchBtn setOn:[self.settingDict[kSettingVoiceOn] boolValue] animated:NO];
    [switchBtn addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:switchBtn];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, CGRectGetMinX(switchBtn.frame) - 16, 22)];
    lbl.text = @"声音";
    lbl.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    lbl.textColor = makeColorHex(@"#333333");
    lbl.center = CGPointMake(lbl.center.x, switchBtn.center.y);
    [view addSubview:lbl];
    
    return view;
}

- (void)handleSwitch:(UISwitch *)switchBtn {
    NSInteger tag = switchBtn.tag;
    switch (tag) {
        case 11:
            {
                [self.settingDict setObject:@(switchBtn.on) forKey:kSettingVoiceOn];
            }
            break;
            
        default:
            break;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:self.settingDict forKey:kVoiceAssistantSetting];
    [self refreshButtons];
}

- (UIView *)formView2 {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 48, kScreenWidth, 48)];
    
    self.btnVoiceSpeed1 = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - (44 + 16) * 3, 0, 44, 20)];
    self.btnVoiceSpeed1.center = CGPointMake(self.btnVoiceSpeed1.center.x, view.frame.size.height / 2);
    self.btnVoiceSpeed1.tag = 21;
    [self.btnVoiceSpeed1 setTitle:@"1倍" forState:UIControlStateNormal];
    [self.btnVoiceSpeed1.titleLabel setFont:[UIFont fontWithName:@"PingFang SC" size:14]];
    self.btnVoiceSpeed1.layer.cornerRadius = 2;
    self.btnVoiceSpeed1.layer.borderColor = kHighlightColor.CGColor;
    self.btnVoiceSpeed1.layer.borderWidth = 1;
    [self.btnVoiceSpeed1 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.btnVoiceSpeed1];
    
    self.btnVoiceSpeed2 = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - (44 + 16) * 2, 0, 44, 20)];
    self.btnVoiceSpeed2.center = CGPointMake(self.btnVoiceSpeed2.center.x, view.frame.size.height / 2);
    self.btnVoiceSpeed2.tag = 22;
    [self.btnVoiceSpeed2 setTitle:@"1.5倍" forState:UIControlStateNormal];
    [self.btnVoiceSpeed2.titleLabel setFont:[UIFont fontWithName:@"PingFang SC" size:14]];
    self.btnVoiceSpeed2.layer.cornerRadius = 2;
    self.btnVoiceSpeed2.layer.borderColor = kHighlightColor.CGColor;
    self.btnVoiceSpeed2.layer.borderWidth = 1;
    [self.btnVoiceSpeed2 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.btnVoiceSpeed2];
    
    self.btnVoiceSpeed3 = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - (44 + 16), 0, 44, 20)];
    self.btnVoiceSpeed3.center = CGPointMake(self.btnVoiceSpeed3.center.x, view.frame.size.height / 2);
    self.btnVoiceSpeed3.tag = 23;
    [self.btnVoiceSpeed3 setTitle:@"2倍" forState:UIControlStateNormal];
    [self.btnVoiceSpeed3.titleLabel setFont:[UIFont fontWithName:@"PingFang SC" size:14]];
    self.btnVoiceSpeed3.layer.cornerRadius = 2;
    self.btnVoiceSpeed3.layer.borderColor = kHighlightColor.CGColor;
    self.btnVoiceSpeed3.layer.borderWidth = 1;
    [self.btnVoiceSpeed3 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.btnVoiceSpeed3];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, CGRectGetMinX(self.btnVoiceSpeed1.frame) - 16, 22)];
    lbl.text = @"语速";
    lbl.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    lbl.textColor = makeColorHex(@"#333333");
    lbl.center = CGPointMake(lbl.center.x, self.btnVoiceSpeed1.center.y);
    [view addSubview:lbl];
    
    [self refreshButtons];
    
    return view;
}

- (UIView *)formView3 {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 48 * 2, kScreenWidth, 48)];
    
    self.btnVoiceTag1 = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - (44 + 16) * 2, 0, 44, 20)];
    self.btnVoiceTag1.center = CGPointMake(self.btnVoiceTag1.center.x, view.frame.size.height / 2);
    self.btnVoiceTag1.tag = 31;
    [self.btnVoiceTag1 setTitle:@"男声" forState:UIControlStateNormal];
    [self.btnVoiceTag1.titleLabel setFont:[UIFont fontWithName:@"PingFang SC" size:14]];
    self.btnVoiceTag1.layer.cornerRadius = 2;
    self.btnVoiceTag1.layer.borderColor = kHighlightColor.CGColor;
    self.btnVoiceTag1.layer.borderWidth = 1;
    [self.btnVoiceTag1 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.btnVoiceTag1];
    
    self.btnVoiceTag2 = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - (44 + 16), 0, 44, 20)];
    self.btnVoiceTag2.center = CGPointMake(self.btnVoiceTag2.center.x, view.frame.size.height / 2);
    self.btnVoiceTag2.tag = 32;
    [self.btnVoiceTag2 setTitle:@"女声" forState:UIControlStateNormal];
    [self.btnVoiceTag2.titleLabel setFont:[UIFont fontWithName:@"PingFang SC" size:14]];
    self.btnVoiceTag2.layer.cornerRadius = 2;
    self.btnVoiceTag2.layer.borderColor = kHighlightColor.CGColor;
    self.btnVoiceTag2.layer.borderWidth = 1;
    [self.btnVoiceTag2 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.btnVoiceTag2];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, CGRectGetMinX(self.btnVoiceTag1.frame) - 16, 22)];
    lbl.text = @"更换声音";
    lbl.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    lbl.textColor = makeColorHex(@"#333333");
    lbl.center = CGPointMake(lbl.center.x, self.btnVoiceTag1.center.y);
    [view addSubview:lbl];
    
    [self refreshButtons];
    
    return view;
}

- (void)handleButton:(UIButton *)button {
    BOOL voiceOn = [self.settingDict[kSettingVoiceOn] boolValue];
    if (!voiceOn) {
        return;
    }
    NSInteger tag = button.tag;
    switch (tag) {
        case 21:
        {
            [self.settingDict setObject:@1 forKey:kSettingVoiceSpeed];
        }
            break;
        case 22:
        {
            [self.settingDict setObject:@1.5 forKey:kSettingVoiceSpeed];
        }
            break;
        case 23:
        {
            [self.settingDict setObject:@2 forKey:kSettingVoiceSpeed];
        }
            break;
            
        case 31:
        {
            [self.settingDict setObject:@"男声" forKey:kSettingVoiceTag];
        }
            break;
        case 32:
        {
            [self.settingDict setObject:@"女声" forKey:kSettingVoiceTag];
        }
            break;
            
        default:
            break;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:self.settingDict forKey:kVoiceAssistantSetting];
    [self refreshButtons];
}

- (UIColor *)statusBackgroundColor:(BOOL)status {
    BOOL voiceOn = [self.settingDict[kSettingVoiceOn] boolValue];
    return voiceOn ? (status ? kHighlightColor: kNormalColor) : kNormalColor;
}

- (UIColor *)statusTitleColor:(BOOL)status {
    BOOL voiceOn = [self.settingDict[kSettingVoiceOn] boolValue];
    return voiceOn ? (status ? kNormalColor: kHighlightColor) : kHighlightColor;
}

- (void)refreshButtons {
    NSString *voiceSpeed = self.settingDict[kSettingVoiceSpeed];
    [self.btnVoiceSpeed1 setBackgroundColor:[self statusBackgroundColor:voiceSpeed.floatValue == 1]];
    [self.btnVoiceSpeed1 setTitleColor:[self statusTitleColor:voiceSpeed.floatValue == 1] forState:UIControlStateNormal];
    
    [self.btnVoiceSpeed2 setBackgroundColor:[self statusBackgroundColor:voiceSpeed.floatValue == 1.5]];
    [self.btnVoiceSpeed2 setTitleColor:[self statusTitleColor:voiceSpeed.floatValue == 1.5] forState:UIControlStateNormal];
    
    [self.btnVoiceSpeed3 setBackgroundColor:[self statusBackgroundColor:voiceSpeed.floatValue == 2]];
    [self.btnVoiceSpeed3 setTitleColor:[self statusTitleColor:voiceSpeed.floatValue == 2] forState:UIControlStateNormal];
    
    NSString *voiceTag = self.settingDict[kSettingVoiceTag];
    [self.btnVoiceTag1 setBackgroundColor:[self statusBackgroundColor:[voiceTag isEqualToString:@"男声"]]];
    [self.btnVoiceTag1 setTitleColor:[self statusTitleColor:[voiceTag isEqualToString:@"男声"]] forState:UIControlStateNormal];
    
    [self.btnVoiceTag2 setBackgroundColor:[self statusBackgroundColor:[voiceTag isEqualToString:@"女声"]]];
    [self.btnVoiceTag2 setTitleColor:[self statusTitleColor:[voiceTag isEqualToString:@"女声"]] forState:UIControlStateNormal];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
